const db = require('../db/db')
const {paging} = require('../utils/paging')
module.exports = {
    // 获取套餐
    GETPACKAGE: async (ctx, next) => {
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop} = ctx.request.query
        try {
            let [res] = await db.promise().query('select id,packageName,packagePrice,packageImg,packageNum from package where belongToShop=? order by createTime desc limit ?,?',
            [belongToShop,startPage,endPage])
            let [total] = await db.promise().query('select * from package')
            ctx.body ={
                code:200,
                msg:'套餐获取成功',
                packageData:res,
                total:total.length
            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'套餐获取失败'+error
            }
        }
        
    },
    // 添加套餐
    ADDPACKAGE: async (ctx, next) => {
        const {id,packageName,packagePrice,packageImg,packageNum,belongToShop } = ctx.request.body
        const createTime = new Date()
        try {
            let [res] = await db.promise().query('insert into package(id,packageName,packagePrice,packageImg,packageNum,belongToShop,createTime) values(?,?,?,?,?,?,?)',
            [id,packageName,packagePrice,packageImg,packageNum,belongToShop,createTime ])
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'套餐添加成功',
                }            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'套餐添加失败'+error,
            }
        }
    },
    // 修改套餐
    UPDATEPACKAGE: async (ctx, next) => {
        const {id,packageName,packagePrice,packageImg,packageNum} = ctx.request.body
        try {
            let [res] =await db.promise().query('update package set packageName=?,packagePrice=?,packageImg=?,packageNum=? where id=?',
            [packageName,packagePrice,packageImg,packageNum,id])
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'套餐修改成功',
                }            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'套餐修改失败'+error,
            }
        }
        
        
    },
    // 删除套餐
    DELPACKAGE: async (ctx, next) => {
        const {id} = ctx.request.body
        try {
            let [res] = await db.promise().query('delete from package where id=?',id)
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'套餐删除成功',
                }           
             }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'套餐删除失败'+error,
            }
        }
       
    },
    // 搜索套餐
    SEARCHPACKAGE: async (ctx, next) => {
        const {keyWord,belongToShop} = ctx.request.body
        try {
            let [res] = await db.promise().query(`select id,packageName,packagePrice,packageImg,packageNum from package where packageName like "%${keyWord}%"and belongToShop=?` 
            ,[belongToShop])
           if(res.length > 0){
            ctx.body ={
                code:200,
                msg:'搜索套餐成功',
                packageList:res,
                total:res.length
            }
           }else{
            ctx.body ={
                code:200,
                msg:'没有该套餐',
            }
           }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'搜索套餐失败'+error,
            }
        }
    },
}